/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Util;
import Pojo.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author An
 */
public class All_Ham_XuLyCauHoi {
    public static int mangluucauhoidara[];
    public static int madethi;
    public static List<Capdo> LayCapdoTheoMacapdo(String macapdo)
    {
        List<Capdo> dscapdo = null;
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "from Capdo cd where cd.macapdo = :macapdo";
            Query query = session.createQuery(hql);
            query.setParameter("macapdo",macapdo);
            dscapdo = query.list();
        }finally{
            session.close();
        }       
        return dscapdo;
    }
    public static void KhoiTaoMangLuuCauHoiDaRa(int tongcau)
    {
        for(int i=0;i<tongcau;i++)
        {
            mangluucauhoidara[i] = -1;
        }
    }
    public static ArrayList<CauHoi> dsch = CauHoiDAO.layTatCaCauHoi();
    public static boolean KiemTraCauDoDaRaChua(int tongcau, int n, int cau,int dokho)
    {
        boolean kq = false;
        for(int i=0;i<tongcau;i++)
        {
            if(mangluucauhoidara[i] == cau || dsch.get(cau).getDoKho() != dokho)
                return true;
        } 
        return kq;
    }
    public static int RanDomCauHoiKhongTrung(int tongcau, int n,int k, int dokho)
    {
        boolean flag = true;
        Random rand = new Random();
        int cau = 0;
        while(flag==true){
            cau = rand.nextInt(n);
            if(KiemTraCauDoDaRaChua(tongcau,n,cau,dokho)==false){
                mangluucauhoidara[k] = cau;
                flag = false;
            }
        }
        return cau;
    }
    
    public static boolean KiemTraMaDeTonTai(String made)
    {
        int made1 = Integer.parseInt(made); 
        List<Dethi> ds = null; 
        boolean kq = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "from Dethi dt where dt.maDeThi = :made1";             
            Query query = session.createQuery(hql); 
            query.setParameter("made1", made1);
            ds = query.list();
           if(ds.size()>0){
               kq = true;
           }
        }catch (HibernateException ex){
            System.err.print(ex);
        }
        return kq;
    }
    public static String RandomMaDe()
    {
        Random rand = new Random();
        int n = 1000;
        int made;
        String md = null;
        boolean flag = true;
        while(flag==true)
        {
            made = rand.nextInt(n);
            md = String.valueOf(made);
            if(KiemTraMaDeTonTai(md)==false)
            {
                flag = false;
                madethi = Integer.parseInt(md);
                return md;
            }   
        }
        return md;
    }
    public static LuuDSCauHoi_String_Pojo dscauhoi_string = new LuuDSCauHoi_String_Pojo();
    public static ArrayList<CauHoi> LayDanhSachCauHoiTheoCapDo(Capdo capdo)
    {
        KhoiTaoMangLuuCauHoiDaRa(capdo.getTongsocau());
        ArrayList<CauHoi> cauhoi = new ArrayList<>();
        int k=0;
        String kho;
        String de;
        String trungbinh;
        kho = "";
        trungbinh = "";
        de = "";
        for(int i=0;i<capdo.getKho();i++)
        {
            int cau = RanDomCauHoiKhongTrung(capdo.getTongsocau(),dsch.size(),k,1);
            kho += String.valueOf(cau) + ",";
            dscauhoi_string.setKho(kho); 
            cauhoi.add(dsch.get(cau));
            k++;
        }
        for(int i=0;i<capdo.getTrungbinh();i++)
        {
            int cau = RanDomCauHoiKhongTrung(capdo.getTongsocau(),dsch.size(),k,2);
            trungbinh += String.valueOf(cau)+",";
            dscauhoi_string.setTrungbinh(trungbinh);
            cauhoi.add(dsch.get(cau));
            k++;
        }
        for(int i=0;i<capdo.getDe();i++)
        {
            int cau = RanDomCauHoiKhongTrung(capdo.getTongsocau(),dsch.size(),k,3);
            de += String.valueOf(cau)+",";
            dscauhoi_string.setDe(de);
            cauhoi.add(dsch.get(cau));
            k++;
        }
        return cauhoi;
    }
    public static LuuDSCauHoi_String_Pojo LuuDSCauHoi_String(Capdo capdo){
        LayDanhSachCauHoiTheoCapDo(capdo);
        return dscauhoi_string;
    }
    public static ArrayList<CauHoi> LayCauHoiTheoCapDo(Capdo capdo)
    {
        ArrayList<CauHoi> ds = null;
        ds = LayDanhSachCauHoiTheoCapDo(capdo);
        return ds;
    }
    
    public static float DiemThi(List<CauHoi> dsch, int dapan[])
    {
        float diem = 0;
        float diemcua1cau = (float)10/dsch.size();
        for(int i=0;i<dsch.size();i++)
        {
            CauHoi ch = dsch.get(i);
            if(ch.getCauDung() == dapan[i])
            {
                diem = (float) (diem + diemcua1cau);
            }
        }
        return diem;
    }
    
    public static int SoCauDung(List<CauHoi> dsch, int dapan[])
    {
        int caudung = 0;
        for(int i=0;i<dsch.size();i++)
        {
            CauHoi ch = dsch.get(i);
            if(ch.getCauDung() == dapan[i])
            {
                caudung++;
            }
        }
        return caudung;
    }
      public static int SoCauSai(List<CauHoi> dsch, int dapan[])
    {
        int socausai = dsch.size() - SoCauDung(dsch, dapan);
        return socausai;
    }
}
